package com.yiwei.zhh.view;

import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.yiwei.zhh.adapter.AutoPollAdapter;
import com.yiwei.zhh.adapter.CardAdapter;
import com.yiwei.zhh.model.DealTypeModel;
import com.yiwei.zhh.model.DealTypesModel;
import com.yiwei.zhh.view.animation.Techniques;
import com.yiwei.zhh.view.animation.YoYo;
import com.lljjcoder.view.ViewHelper;

import java.util.ArrayList;
import java.util.List;


public class ShadowTransformer implements ViewPager.OnPageChangeListener, ViewPager.PageTransformer {

    private List<String> datas;
    private ViewPager mViewPager;
    private CardAdapter mAdapter;
    private float mLastOffset;
    private boolean mScalingEnabled;
    private WheelView mTabLayout;
    private List<DealTypesModel.DataBean> dataLists;
    //    int itemCount = 0;
    //    private String[] values = new String[]{"500元", "600元", "700元", "800元", "900元", "1000元", "1500元", "3000元"};
//    private String[] values;
    private List<String> singleList;
    private List<Integer> singleNumberList;
    private List<Integer> singleAddNumList;

    public ShadowTransformer(int countData, List<DealTypesModel.DataBean> dataLists, ViewPager viewPager, CardAdapter adapter, WheelView mTabLayout) {
        this.dataLists = dataLists;
        if (dataLists == null) {
            return;
        }
        datas = new ArrayList<>();
        singleList = new ArrayList<>();
        singleNumberList = new ArrayList<>();
        singleAddNumList = new ArrayList<>();
//        获取每一个金额
        for (int i = 0; i < dataLists.size(); i++) {
            if (i != 0 && dataLists.get(i).borrow_amount == dataLists.get(i - 1).borrow_amount) {
                continue;
            }
            singleList.add(dataLists.get(i).borrow_amount + "元");
        }

//        获取单个金额有多少个不同的时间期限
        for (int j = 0; j < singleList.size(); j++) {
            int num = 0;
            for (int i = 0; i < dataLists.size(); i++) {
//                判断当前金额是否一样，是的话就+1
                if ((dataLists.get(i).borrow_amount + "元").equals(singleList.get(j))) {
                    num++;
                }
            }
            singleNumberList.add(num);
        }

//   获取当前金额的坐标
        int addNum = 0;
        for (int i = 0; i < singleNumberList.size(); i++) {
//            if (i == 0) {
//                addNum = 0;
//            } else {
            addNum += singleNumberList.get(i);
//            }
            singleAddNumList.add(addNum);
        }


//        数目为10倍的金额选项，方便左右循环
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < dataLists.size(); i++) {
                if (i != 0 && dataLists.get(i).borrow_amount == dataLists.get(i - 1).borrow_amount) {
                    continue;
                }
                datas.add(dataLists.get(i).borrow_amount + "元");
            }
        }
        /*
        * itemCount=每个tab选项有多少个fragment
        * count=有多少个tab选项
        * */
//        itemCount = 2;
//        itemCount = countData / (datas.size() / 10);
//        int count = dataLists.size() / itemCount;
        mViewPager = viewPager;
        viewPager.addOnPageChangeListener(this);
        mAdapter = adapter;
        this.mTabLayout = mTabLayout;

        mTabLayout.setItems(datas);
        mTabLayout.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onWheelItemSelected(int position) {
                //                **********测试左右循环*********
                /*
                * select:当前的第几个Tab选项
                * poi=当前选中的fragment的POI
                * */
//                int select = position % (count);
//                int poi = itemCount * select;
                int select = position % (singleList.size());
//                int poi = itemCount * select;


                if (select == 0) {
                    mTabLayout.selectIndex(singleList.size() * 5);
                    mViewPager.setCurrentItem(1);
                } else if (select == dataLists.size() + 1) {
                    mViewPager.setCurrentItem(dataLists.size() - 1);
                } else {

//                    mViewPager.setCurrentItem(select + 1);
//                    if(singleNumberList.get(select)==1){
//                        mViewPager.setCurrentItem(singleAddNumList.get(select)+2);
//                    }else {
                    mViewPager.setCurrentItem((singleAddNumList.get(select) + 1 - singleNumberList.get(select)));
//                    }

                }
                if (position == datas.size() - 1) {
                    mTabLayout.selectIndex(singleList.size() * 5 + select);
                }
                //                **********测试左右循环*********

            }
        });

    }

    public void enableScaling(boolean enable) {
        if (mScalingEnabled && !enable) {
            // shrink main card
            CardView currentCard = mAdapter.getCardViewAt(mViewPager.getCurrentItem());
            if (currentCard != null) {
                currentCard.animate().scaleY(1);
                currentCard.animate().scaleX(1);
            }
        } else if (!mScalingEnabled && enable) {
            // grow main card
            CardView currentCard = mAdapter.getCardViewAt(mViewPager.getCurrentItem());
            if (currentCard != null) {
                currentCard.animate().scaleY(1.1f);
                currentCard.animate().scaleX(1.1f);
            }
        }

        mScalingEnabled = enable;
    }


    @Override
    public void transformPage(View view, float position) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        int realCurrentPosition;
        int nextPosition;
        float baseElevation = mAdapter.getBaseElevation();
        float realOffset;
        boolean goingLeft = mLastOffset > positionOffset;

        // If we're going backwards, onPageScrolled receives the last position
        // instead of the current one
        if (goingLeft) {
            realCurrentPosition = position + 1;
            nextPosition = position;
            realOffset = 1 - positionOffset;
        } else {
            nextPosition = position + 1;
            realCurrentPosition = position;
            realOffset = positionOffset;
        }

        // Avoid crash on overscroll
        if (nextPosition > mAdapter.getCount() - 1
                || realCurrentPosition > mAdapter.getCount() - 1) {
            return;
        }

        CardView currentCard = mAdapter.getCardViewAt(realCurrentPosition);

        // This might be null if a fragment is being used
        // and the views weren't created yet
        if (currentCard != null) {
            if (mScalingEnabled) {
                currentCard.setScaleX((float) (1 + 0.1 * (1 - realOffset)));
                currentCard.setScaleY((float) (1 + 0.1 * (1 - realOffset)));
            }
//            currentCard.setCardElevation((baseElevation + baseElevation
//                    * (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (1 - realOffset)));
        }

        CardView nextCard = mAdapter.getCardViewAt(nextPosition);

        // We might be scrolling fast enough so that the next (or previous) card
        // was already destroyed or a fragment might not have been created yet
        if (nextCard != null) {
            if (mScalingEnabled) {
                nextCard.setScaleX((float) (1 + 0.1 * (realOffset)));
                nextCard.setScaleY((float) (1 + 0.1 * (realOffset)));
            }
//            nextCard.setCardElevation((baseElevation + baseElevation
//                    * (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (realOffset)));
        }

        mLastOffset = positionOffset;
    }

    @Override
    public void onPageSelected(int position) {
        //                **********测试左右循环*********
        if (position == 0) {
            /*
            * positon=0,替换为最后一个
            * */
            mViewPager.setCurrentItem(dataLists.size(), false);
//            mTabLayout.selectIndex((dataLists.size()) / itemCount);
            return;
        } else if (position == dataLists.size() + 1) {
                    /*
            * positon=最后一个,跳转为POI=1的Fragment
            * */
            mViewPager.setCurrentItem(1, false);
//            mTabLayout.selectIndex(0);
            return;
        }
//        mTabLayout.selectIndex(((position - 1) / itemCount) + singleList.size() * 5);
        //                **********测试左右循环*********

        for (int i = 0; i < singleAddNumList.size(); i++) {
            if (position <= singleAddNumList.get(i)) {
                mTabLayout.selectIndex(singleList.size() * 5 + i);
                break;
            }
        }


    }

//    private boolean isScrolled = false;

    @Override
    public void onPageScrollStateChanged(int status) {

//        switch (status) {
//            case 1:// 手势滑动
//                isScrolled = false;
//                break;
//            case 2:// 界面切换
//                isScrolled = true;
//                break;
//            case 0:// 滑动结束
//                break;
//        }
    }

}
